﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
//using SmartWeb.BLL;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using Yipusoft.MapInfo.BLL;

public partial class Map_MarkUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string desc = HttpUtility.HtmlDecode(Request.Params["desc"]);
        string img = HttpUtility.HtmlDecode(Request.Params["img"]);
        if (desc == null)
        {
            desc = string.Empty;
        }
        MarkupImage("Maps/"+img, desc);
    }
    private void MarkupImage(string image, string desc)
    {
        Bitmap bitmap = new Bitmap(Server.MapPath(image));
        MemoryStream memStream = new MemoryStream();
        Graphics g;
        try
        {
            //试图取得Graphics对象，用于添加文字 

            //对于indexed pixel format，此处会抛出异常
            g = System.Drawing.Graphics.FromImage(bitmap);
        }
        catch (Exception)
        {

            //对于indexed pixel format，抛出异常的特殊处理
            Bitmap newBmp = new Bitmap(bitmap.Width, bitmap.Height);
            g = System.Drawing.Graphics.FromImage(newBmp);
            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, newBmp.Width, newBmp.Height));
            g.DrawImage(bitmap,
                               new Rectangle(0, 0, newBmp.Width, newBmp.Height),
                               new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                               GraphicsUnit.Pixel);
            bitmap = newBmp;
        } 


       // Graphics g = Graphics.FromImage(bitmap);
        g.SmoothingMode = SmoothingMode.AntiAlias;

        // Draw each annotation object of MarkableType, ; separated
        if (desc.IndexOf(";") > 0)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;

            string[] xys = desc.Split(';');
            Point[] points = new Point[xys.Length];
            ArrayList marks = new ArrayList(xys.Length);

            int i = 0;
            foreach (string xy in xys)
            {
                // Internally comma separated
                string[] tmp = xy.Split(',');

                if (tmp.Length <= 2)
                {
                    // skip if incomplete pair
                    continue;
                }

                // Get x, y
                int x = int.Parse(tmp[0]);
                int y = int.Parse(tmp[1]);
                points[i++] = new Point(x, y);

                // Get type
                string typeid = tmp[2];

                // Get the rest as the arguments
                string param = string.Empty;
                if (tmp.Length == 4)
                {
                    param = tmp[3];
                }
                else if (tmp.Length > 4)
                {
                    string[] rest = new string[tmp.Length - 3];
                    for (int j = 3; j < tmp.Length; j++)
                    {
                        rest[j - 3] = tmp[j];
                    }
                    param = string.Join(",", rest);
                }

                // Create MarkableType oject from its string form
                MarkableType mark = MarkManager.CreateByID(typeid);
                if (param.Length > 0)
                {
                    mark.LoadFromString(param);
                }
                marks.Add(mark);
            }

            // The number of marks is i
            SolidBrush brush = new SolidBrush(Color.Blue);
            for (int j = 0; j < i; j++)
            {
                string familyName = "Tahoma";

                Font font;
                font = new Font(familyName, 10F, FontStyle.Bold);

                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                // Draw an identifier
                g.DrawString(j.ToString(), font, brush,
                    new Point(points[j].X + 12, points[j].Y), format);

                // Draw the annotation itself
                MarkManager.Draw(g, points[j].X, points[j].Y, (MarkableType)marks[j]);
            }
            brush.Dispose();
        }

        // Set the content type
        Response.Clear();
        Response.ContentType = "image/jpeg";

        // Send the bitmap to the output stream
        bitmap.Save(memStream, ImageFormat.Jpeg);
        memStream.WriteTo(Response.OutputStream);

        // Cleanup
        g.Dispose();
        bitmap.Dispose();
    }
}

